﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
/// <summary>
/// GridView绑定字段前处理
/// </summary>
public static class GridViewUtil
{
    /// <summary>
    /// 将不存在于DataTable的GridView列移除函数
    /// </summary>
    /// <param name="gv">待处理的GridView</param>
    /// <param name="dt">绑定数据源</param>
    public static void AdjustGridView(GridView gv, DataTable dt)
    {
        // 获取字段列表
        List<string> fieldslist = new List<string>();
        foreach (DataColumn item in dt.Columns)
            fieldslist.Add(item.ColumnName);

        // 获取待删除列表

        List<DataControlField> listtobedel = new List<DataControlField>();
        foreach (var item in gv.Columns)
        {
            if (item is BoundField)
            {
                if (!fieldslist.Contains((item as BoundField).DataField))
                {

                    BoundField bf = item as BoundField;
                    bf.Visible = false;
                    bf.DataField = "POS";
                    // listtobedel.Add(item as BoundField);
                }
            }

        }

        // 删除不存在数据源的列
        foreach (var item in listtobedel)
        {
            gv.Columns.Remove(item);
        }
    }
}
/// <summary>
/// 列显示Session传递对象
/// </summary>
public class ShowColumns
{
    public GroupFields GroupFields { get; set; }
    public static ShowColumns ShowColumn
    {
        get
        {
            return System.Web.HttpContext.Current.Session["ShowColumns"] as ShowColumns;

        }
        set
        {
            System.Web.HttpContext.Current.Session["ShowColumns"] = value;
        }
    }
}
